package com.msconfig.mvp.view.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.constraint.ConstraintLayout;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.geo.msconfig.R;
import com.msconfig.bean.deviceInfoBean.TagDeviceInfo;
import com.msconfig.device.CommandParseManage;
import com.msconfig.device.CommandSendManage;
import com.msconfig.device.DeviceManage;
import com.msconfig.device.GetSendCommandList;
import com.msconfig.device.TagSendCommand;
import com.msconfig.event.DeviceUpdateEvent;
import com.msconfig.event.OtherCommunicationDialogDismissEvent;
import com.msconfig.event.PowerOffElectricQuantityDialogDismissEvent;
import com.msconfig.event.PowerOnDataDialogDismissEvent;
import com.msconfig.event.PowerOnTimeDialogDismissEvent;
import com.msconfig.event.TimeZoneDialogDismissEvent;
import com.msconfig.event.WifiFrequencyBandDialogDismissEvent;
import com.msconfig.event.WorkerHoursDialogDismissEvent;
import com.msconfig.mvp.view.activity.SetupOtherCommunicationDialogActivity;
import com.msconfig.mvp.view.activity.SetupSurveyDataLinkDialogActivity;
import com.msconfig.mvp.view.activity.SetupSurveyPowerOffElectricQuantityDialogActivity;
import com.msconfig.mvp.view.activity.SetupSurveyPowerOnDateDialogActivity;
import com.msconfig.mvp.view.activity.SetupSurveyPowerOnTimeDialogActivity;
import com.msconfig.mvp.view.activity.SetupSurveyTimeIntervalDialogActivity;
import com.msconfig.mvp.view.activity.SetupSurveyWorkerHoursDialogActivity;
import com.msconfig.mvp.view.activity.base.BaseMvpFragment;
import com.msconfig.mvp.view.activity.base.BasePresenter;
import com.msconfig.utils.BaseConversionUtil;
import com.msconfig.utils.IsNumAndPointUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import static android.view.View.VISIBLE;

public class SetupSurveyStationFragment extends BaseMvpFragment implements View.OnClickListener {
    public static final String RECORDINGINTERVAL = "RecordingInterval";
    public static final String DATALINK = "rlDataLink";
    public static final String COMMUNICATIONSETUP = "rlCommunicationSetup";
    private ImageView ivCommonBack;
    private TextView tvCommonTitle;
    private FrameLayout flLoading;
    private ImageView ivLoading;
    private ConstraintLayout clCommunication;
    private RelativeLayout rlSurveyName;
    private EditText etSurveyName;
    private RelativeLayout rlRecordingInterval;
    private TextView tvRecordingInterval;
    private RelativeLayout rlDataLink;
    private TextView tvDataLink;
    private RelativeLayout rlFirstIp;
    private EditText etFirstIp;
    private RelativeLayout rlFirstPort;
    private EditText etFirstPort;
    private RelativeLayout rlStartDoubleStation;
    private ImageView ivSwitchDoubleStationOn;
    private ImageView ivSwitchDoubleStationOff;
    private RelativeLayout rlSecondStation;
    private RelativeLayout rlSecondIp;
    private EditText etSecondIp;
    private RelativeLayout rlSecondPort;
    private EditText etSecondPort;
    private RelativeLayout rlResultIp;
    private EditText etResultIp;
    private RelativeLayout rlResultPort;
    private EditText etResultPort;
    private RelativeLayout rlStartTimingSwitch;
    private ImageView ivSwitchTimingOn;
    private ImageView ivSwitchTimingOff;
    private RelativeLayout rlExternalSensor;
    private ImageView ivSwitchExternalSensorOn;
    private ImageView ivSwitchExternalSensorOff;
    private RelativeLayout rlEarthDisaster;
    private ImageView ivSwitchEarthDisasterOn;
    private ImageView ivSwitchEarthDisasterOff;
    private RelativeLayout rlCommunicationSetup;
    private TextView tvCommunicationSetup;
    private RelativeLayout rlServerAdress;
    private EditText etServerAdress;
    private RelativeLayout rlPort;
    private EditText etServerPort;
    private Button btnDetermine;
    private ConstraintLayout clPowerOn;
    private RelativeLayout rlPowerOnData;
    private TextView tvPowerOnData;
    private RelativeLayout rlPowerOnTime;
    private TextView tvPowerOnTime;
    private RelativeLayout rlWorkerHours;
    private TextView tvWorkerHours;
    private RelativeLayout rlPowerOffElectricQuantity;
    private TextView tvPowerOffElectricQuantity;
    private int type;
    private Context context;
    private TagDeviceInfo deviceInfo;


    private MyHandler myHandler;
    private boolean isSetupDefail;
    protected static final int MSG_SET_UP_DEFAIL =1;//设置失败
    private Animation setupAnim;


    //负责更新UI
    private static class MyHandler extends Handler {
        WeakReference<SetupSurveyStationFragment> weakReference;
        private MyHandler(SetupSurveyStationFragment fragment) {
            weakReference = new WeakReference<SetupSurveyStationFragment>(fragment);
        }
        @Override
        public void handleMessage(Message msg) {
            SetupSurveyStationFragment fragment = weakReference.get();
            if (fragment != null) {
                switch (msg.what) {
                    case MSG_SET_UP_DEFAIL:
                        if(fragment.flLoading.getVisibility()==VISIBLE){//动画还在，说明设置事件过久设置失败
                            fragment.stopAnim();
                            fragment.isSetupDefail=true;
                            fragment.showToast(R.string.toast_setup_defail);
                        }
                        break;
                }
            }
        }
    }

    public static SetupSurveyStationFragment newInstance() {
        Bundle args = new Bundle();
        SetupSurveyStationFragment fragment = new SetupSurveyStationFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.layout_fragment_setup_survey_station;
    }

    @Override
    protected void initView(View view) {
        ivCommonBack=view.findViewById(R.id.iv_common_back);
        tvCommonTitle=view.findViewById(R.id.tv_common_title);
        flLoading=view.findViewById(R.id.fl_loading);
        ivLoading = view.findViewById(R.id.iv_loading);
        clCommunication=(ConstraintLayout) view.findViewById(R.id.cl_communication);
        rlSurveyName = (RelativeLayout)view.findViewById( R.id.rl_survey_name );
        etSurveyName = (EditText)view.findViewById( R.id.et_survey_name );
        rlRecordingInterval = (RelativeLayout)view.findViewById( R.id.rl_recording_interval );
        tvRecordingInterval = (TextView)view.findViewById( R.id.tv_recording_interval );
        rlDataLink = (RelativeLayout)view.findViewById( R.id.rl_data_link );
        tvDataLink = (TextView)view.findViewById( R.id.tv_data_link );
        rlFirstIp = (RelativeLayout)view.findViewById( R.id.rl_first_ip );
        etFirstIp = (EditText)view.findViewById( R.id.et_first_ip );
        rlFirstPort = (RelativeLayout)view.findViewById( R.id.rl_first_port );
        etFirstPort = (EditText)view.findViewById( R.id.et_first_port );
        rlStartDoubleStation = (RelativeLayout)view.findViewById( R.id.rl_start_double_station );
        ivSwitchDoubleStationOn = (ImageView)view.findViewById( R.id.iv_switch_double_station_on );
        ivSwitchDoubleStationOff = (ImageView)view.findViewById( R.id.iv_switch_double_station_off );
        rlSecondStation = (RelativeLayout)view.findViewById( R.id.rl_second_station );
        rlSecondIp = (RelativeLayout)view.findViewById( R.id.rl_second_ip );
        etSecondIp = (EditText)view.findViewById( R.id.et_second_ip );
        rlSecondPort = (RelativeLayout)view.findViewById( R.id.rl_second_port );
        etSecondPort = (EditText)view.findViewById( R.id.et_second_port );
        rlResultIp = (RelativeLayout)view.findViewById( R.id.rl_result_ip );
        etResultIp = (EditText)view.findViewById( R.id.et_result_ip );
        rlResultPort = (RelativeLayout)view.findViewById( R.id.rl_result_port );
        etResultPort = (EditText)view.findViewById( R.id.et_result_port );
        rlStartTimingSwitch = (RelativeLayout)view.findViewById( R.id.rl_start_timing_switch );
        ivSwitchTimingOn = (ImageView)view.findViewById( R.id.iv_switch_timing_on );
        ivSwitchTimingOff = (ImageView)view.findViewById( R.id.iv_switch_timing_off );
        rlExternalSensor = (RelativeLayout)view.findViewById( R.id.rl_external_sensor );
        ivSwitchExternalSensorOn = (ImageView)view.findViewById( R.id.iv_switch_external_sensor_on );
        ivSwitchExternalSensorOff = (ImageView)view.findViewById( R.id.iv_switch_external_sensor_off );
        rlEarthDisaster = (RelativeLayout)view.findViewById( R.id.rl_earth_disaster );
        ivSwitchEarthDisasterOn = (ImageView)view.findViewById( R.id.iv_switch_earth_disaster_on );
        ivSwitchEarthDisasterOff = (ImageView)view.findViewById( R.id.iv_switch_earth_disaster_off );
        rlCommunicationSetup = (RelativeLayout)view.findViewById( R.id.rl_communication_setup );
        tvCommunicationSetup = (TextView)view.findViewById( R.id.tv_communication_setup );
        rlServerAdress = (RelativeLayout)view.findViewById( R.id.rl_server_adress );
        etServerAdress = (EditText)view.findViewById( R.id.et_server_adress );
        rlPort = (RelativeLayout)view.findViewById( R.id.rl_port );
        etServerPort = (EditText)view.findViewById( R.id.et_port );
        btnDetermine = (Button)view.findViewById( R.id.btn_determine );
        clPowerOn = (ConstraintLayout)view.findViewById( R.id.cl_power_on );
        rlPowerOnData = (RelativeLayout)view.findViewById( R.id.rl_power_on_data );
        tvPowerOnData = (TextView)view.findViewById( R.id.tv_power_on_data );
        rlPowerOnTime = (RelativeLayout)view.findViewById( R.id.rl_power_on_time );
        tvPowerOnTime = (TextView)view.findViewById( R.id.tv_power_on_time );
        rlWorkerHours = (RelativeLayout)view.findViewById( R.id.rl_worker_hours );
        tvWorkerHours = (TextView)view.findViewById( R.id.tv_worker_hours );
        rlPowerOffElectricQuantity = (RelativeLayout)view.findViewById( R.id.rl_power_off_electric_quantity );
        tvPowerOffElectricQuantity = (TextView)view.findViewById( R.id.tv_power_off_electric_quantity );

    }

    @Override
    protected void initListener(View view) {
        ivCommonBack.setOnClickListener(this);
        rlSurveyName.setOnClickListener(this);
        rlRecordingInterval.setOnClickListener(this);
        rlDataLink.setOnClickListener(this);
        rlFirstIp.setOnClickListener(this);
        rlFirstPort.setOnClickListener(this);
        ivSwitchDoubleStationOn.setOnClickListener(this);
        ivSwitchDoubleStationOff.setOnClickListener(this);
        rlResultIp.setOnClickListener(this);
        rlResultPort.setOnClickListener(this);
        ivSwitchTimingOff.setOnClickListener(this);
        ivSwitchTimingOn.setOnClickListener(this);
        ivSwitchExternalSensorOff.setOnClickListener(this);
        ivSwitchExternalSensorOn.setOnClickListener(this);
        ivSwitchEarthDisasterOff.setOnClickListener(this);
        ivSwitchEarthDisasterOn.setOnClickListener(this);
        rlCommunicationSetup.setOnClickListener(this);
        rlServerAdress.setOnClickListener(this);
        rlPort.setOnClickListener(this);
        btnDetermine.setOnClickListener(this);
        rlPowerOnData.setOnClickListener(this);
        rlPowerOnTime.setOnClickListener(this);
        rlWorkerHours.setOnClickListener(this);
        rlPowerOffElectricQuantity.setOnClickListener(this);

    }

    @Override
    protected void initData() {
        tvCommonTitle.setText(R.string.title_setup_survey);
        deviceInfo= CommandParseManage.getInstance().getDeviceInfo();
    }

    @Override
    public void onClick(View v) {
        if(v==ivSwitchDoubleStationOff){
            onClickSwitchDoubleStationOff();
        }else if(v==ivSwitchDoubleStationOn){
            onClickSwitchDoubleStationOn();
        }else if(v==ivSwitchTimingOff){
            onClickSwitchTimingOff();
        }else if(v==ivSwitchTimingOn){
            onClickSwitchTimingOn();
        }else if(v==ivSwitchExternalSensorOff){
            onClickSwitchExternalSensorOff();
        }else if(v==ivSwitchExternalSensorOn){
            onClickSwitchExternalSensorOn();
        }else if(v==ivSwitchEarthDisasterOff){
            onClickSwitchEarthDisasterOff();
        }else if(v==ivSwitchEarthDisasterOn){
            onClickSwitchEarthDisasterOn();
        }else if(v==rlCommunicationSetup){
            Intent intent=new Intent(getActivity(), SetupOtherCommunicationDialogActivity.class);
            intent.putExtra(COMMUNICATIONSETUP,tvCommunicationSetup.getText().toString().trim());
            startActivity(intent);
        }else if(v==ivCommonBack){
            backTo();
        }else if(v==rlRecordingInterval){
            Intent intent=new Intent(getActivity(), SetupSurveyTimeIntervalDialogActivity.class);
            intent.putExtra(RECORDINGINTERVAL,tvRecordingInterval.getText().toString().trim());
            startActivity(intent);
        }else if(v==rlDataLink){
            Intent intent=new Intent(getActivity(), SetupSurveyDataLinkDialogActivity.class);
            intent.putExtra(DATALINK,tvDataLink.getText().toString().trim());
            startActivity(intent);
        }else if(v==btnDetermine){
            onClickBtnDetermine();
        }else if(v==rlPowerOnData){
            Intent intent=new Intent(getActivity(), SetupSurveyPowerOnDateDialogActivity.class);
            startActivity(intent);
        }else if(v==rlPowerOnTime){
            Intent intent=new Intent(getActivity(), SetupSurveyPowerOnTimeDialogActivity.class);
            startActivity(intent);
        }else if(v==rlWorkerHours){
            Intent intent=new Intent(getActivity(), SetupSurveyWorkerHoursDialogActivity.class);
            startActivity(intent);
        }else if(v==rlPowerOffElectricQuantity){
            Intent intent=new Intent(getActivity(), SetupSurveyPowerOffElectricQuantityDialogActivity.class);
            startActivity(intent);
        }

    }

    private void onClickSwitchEarthDisasterOn() {
        ivSwitchEarthDisasterOn.setVisibility(View.GONE);
        ivSwitchEarthDisasterOff.setVisibility(VISIBLE);
    }

    private void onClickSwitchEarthDisasterOff() {
        ivSwitchEarthDisasterOff.setVisibility(View.GONE);
        ivSwitchEarthDisasterOn.setVisibility(VISIBLE);
    }

    private void onClickSwitchExternalSensorOn() {
        ivSwitchExternalSensorOn.setVisibility(View.GONE);
        ivSwitchExternalSensorOff.setVisibility(VISIBLE);
    }

    private void onClickSwitchExternalSensorOff() {
        ivSwitchExternalSensorOff.setVisibility(View.GONE);
        ivSwitchExternalSensorOn.setVisibility(VISIBLE);
    }

    private void onClickSwitchTimingOn() {
        ivSwitchTimingOn.setVisibility(View.GONE);
        ivSwitchTimingOff.setVisibility(VISIBLE);
        clPowerOn.setVisibility(View.GONE);
    }

    private void onClickSwitchTimingOff() {
        ivSwitchTimingOff.setVisibility(View.GONE);
        ivSwitchTimingOn.setVisibility(VISIBLE);
        clPowerOn.setVisibility(VISIBLE);
    }

    private void onClickSwitchDoubleStationOn() {
        ivSwitchDoubleStationOn.setVisibility(View.GONE);
        ivSwitchDoubleStationOff.setVisibility(VISIBLE);
        rlSecondStation.setVisibility(View.GONE);
    }

    private void onClickSwitchDoubleStationOff() {
        ivSwitchDoubleStationOff.setVisibility(View.GONE);
        ivSwitchDoubleStationOn.setVisibility(VISIBLE);
        rlSecondStation.setVisibility(VISIBLE);
    }

    //GETALL成功
    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void showAnalysisGNSSDataSuccess(DeviceUpdateEvent.UpdateDialogGetRtkSuccessStatus event) {
        if (!DeviceManage.getInstance().isConnected())
            return;
        int communicationSetup;
        etSurveyName.setText(deviceInfo.surveyInfo.mSurveyName);
        tvRecordingInterval.setText(Integer.parseInt(deviceInfo.surveyInfo.mRecordingInterval)/3600+"小时");
        if(deviceInfo.surveyInfo.mDataLink.equals("NETWORK")){
            tvDataLink.setText("网络");
        }else{
            tvDataLink.setText("电台");
        }
        etFirstIp.setText(deviceInfo.surveyInfo.mFirstIp);
        etFirstPort.setText(deviceInfo.surveyInfo.mFirstPort);
        etSecondIp.setText(deviceInfo.surveyInfo.mSecondIp);
        etSecondPort.setText(deviceInfo.surveyInfo.mSecondPort);
        if(deviceInfo.surveyInfo.mIsDoubleStation){
            onClickSwitchDoubleStationOff();
        }else{
            onClickSwitchDoubleStationOn();
        }
        etResultIp.setText(deviceInfo.surveyInfo.mResultIp);
        etResultPort.setText(deviceInfo.surveyInfo.mResultPort);
        if(deviceInfo.surveyInfo.mIsStartTiming!=null){
            if(Integer.parseInt(deviceInfo.surveyInfo.mIsStartTiming)==0){
                onClickSwitchTimingOn();
            }else{
                onClickSwitchTimingOff();
                String[] values=deviceInfo.surveyInfo.mIsStartTiming.split("\\|");
                String data= BaseConversionUtil.Decimal2Binary(Integer.parseInt(values[0]));
                String name = "";
                //1111111-星期日-》星期一
                for(int i=data.length()-1;i>=0;i--){
                    if(Integer.parseInt(data.substring(i-1,i))==1){//最后一位星期一
                        switch (i){
                            case 6:
                                name="星期一";
                                break;
                            case 5:
                                name="二";
                                break;
                            case 4:
                                name="三";
                                break;
                            case 3:
                                name="四";
                                break;
                            case 2:
                                name="五";
                                break;
                            case 1:
                                name="六";
                                break;
                            case 0:
                                name="日";
                                break;
                        }

                    }
                    name += "、";
                }
                tvPowerOnData.setText(name);
                tvPowerOnTime.setText(values[1]+":00");
                tvWorkerHours.setText(values[2]+"小时");
                tvPowerOffElectricQuantity.setText(values[3]+"%");
            }
        }

        if(deviceInfo.surveyInfo.mIsExternalSensor){
            onClickSwitchExternalSensorOff();
        }else{
            onClickSwitchExternalSensorOn();
        }
        if(deviceInfo.surveyInfo.mIsEarthDisaster){
            onClickSwitchEarthDisasterOff();
        }else{
            onClickSwitchEarthDisasterOn();
        }
        String type="";
        communicationSetup= Integer.parseInt(deviceInfo.surveyInfo.mCommunicationSetup);
        switch (communicationSetup){
            case 0:
                type=getString(R.string.close);
                clCommunication.setVisibility(View.GONE);
                break;
            case 1:
                type=getString(R.string.tcp_client);
                clCommunication.setVisibility(VISIBLE);
                break;
            case 2:
                clCommunication.setVisibility(VISIBLE);
                type=getString(R.string.tcp_server);
                break;
        }
        tvCommunicationSetup.setText(type);
        etServerAdress.setText(deviceInfo.surveyInfo.mServerAdress);
        etServerPort.setText(deviceInfo.surveyInfo.mServerPort);
    }

    //开始动画
    public void startAnim(){
        setupAnim = AnimationUtils.loadAnimation(context, R.anim.anim_round_rotate);
        LinearInterpolator interpolator = new LinearInterpolator();  //设置匀速旋转，在xml文件中设置会出现卡顿
        setupAnim.setInterpolator(interpolator);
        if (setupAnim != null) {
            btnDetermine.setEnabled(false);
            flLoading.setVisibility(VISIBLE);
            ivLoading.startAnimation(setupAnim);  //开始动画
        }
    }

    //停止动画
    private void stopAnim() {
        if (setupAnim != null) {
            btnDetermine.setEnabled(true);
            setupAnim=null;
            flLoading.setVisibility(View.GONE);
            ivLoading.clearAnimation();
        }
    }

    private void onClickBtnDetermine() {
        isSetupDefail=false;
        if(!DeviceManage.getInstance().isConnected()){
            Toast.makeText(context, R.string.toast_isdisconnect, Toast.LENGTH_SHORT).show();
            return;
        }
        int interval;
        String dataLink;
        String mSurveyName = etSurveyName.getText().toString().trim();
        String mRecordingInterval = tvRecordingInterval.getText().toString().trim();
        interval= Integer.parseInt(mRecordingInterval.substring(0, mRecordingInterval.length()-2))*3600;
        String mDataLink = tvDataLink.getText().toString().trim();
        if(mDataLink.equals("网络")){
            dataLink="NETWORK";
        }else{
            dataLink="UHF";
        }
        String mFirstIp = etFirstIp.getText().toString().trim();
        if(!IsNumAndPointUtil.isNumOrPoint(mFirstIp)){
            Toast.makeText(context, "请输入正确格式的IP地址！", Toast.LENGTH_SHORT).show();
            return;
        }
        String mFirstPort = etFirstPort.getText().toString().trim();
        int doubleStation;
        if(ivSwitchDoubleStationOn.getVisibility()== VISIBLE){
            doubleStation =1;
        }else{
            doubleStation =0;
        }
        String mSecondIp = etSecondIp.getText().toString().trim();
        if(!IsNumAndPointUtil.isNumOrPoint(mSecondIp)){
            Toast.makeText(context, "请输入正确格式的IP地址！", Toast.LENGTH_SHORT).show();
            return;
        }
        String mSecondPort = etSecondPort.getText().toString().trim();
        String mResultIp = etResultIp.getText().toString().trim();
        if(!IsNumAndPointUtil.isNumOrPoint(mResultIp)){
            Toast.makeText(context, "请输入正确格式的IP地址！", Toast.LENGTH_SHORT).show();
            return;
        }
        String mResultPort = etResultPort.getText().toString().trim();
        String powerOnValue;
        if(ivSwitchTimingOn.getVisibility()== VISIBLE){
            int value = 0;
            String date=tvPowerOnData.getText().toString().trim().substring(2);
            String[] dates=date.split("、");
            if(dates.length==-1){//只有一个
                value = GetDecimalNumber(date);
            }else{
                for(int i=0;i<dates.length;i++){
                    value += GetDecimalNumber(dates[i]);
                }
            }
            powerOnValue =value+"|"+tvPowerOnTime.getText().toString().trim().substring(1,2)+"|"
                    +tvWorkerHours.getText().toString().trim().substring(0,1)+"|"
                    +tvPowerOffElectricQuantity.getText().toString().trim().substring(0,2);
        }else{
            powerOnValue ="0";
        }
        int externalSensor;
        if(ivSwitchExternalSensorOn.getVisibility()== VISIBLE){
            externalSensor =1;
        }else{
            externalSensor =0;
        }
        int earthDisaster;
        if(ivSwitchEarthDisasterOn.getVisibility()== VISIBLE){
            earthDisaster =1;
        }else{
            earthDisaster =0;
        }
        String mCommunicationSetup = tvCommunicationSetup.getText().toString().trim();
        if(mCommunicationSetup.equals(getString(R.string.close))){
            type=0;
        }else if(mCommunicationSetup.equals(getString(R.string.tcp_client))){
            type=1;
        }else if(mCommunicationSetup.equals(getString(R.string.tcp_server))){
            type=2;
        }
        String mServerAdress = etServerAdress.getText().toString().trim();
        if(!IsNumAndPointUtil.isNumOrPoint(mServerAdress)){
            Toast.makeText(context, "请输入正确格式的IP地址！", Toast.LENGTH_SHORT).show();
            return;
        }
        String mServerPort = etServerPort.getText().toString().trim();

        //开始设置动画
        startAnim();
        //得到发送命令的集合
        ArrayList<TagSendCommand> sendCommandList= GetSendCommandList.getSurveyStationCommandList(interval, dataLink, mSurveyName, mFirstIp, mFirstPort, doubleStation, mSecondIp, mSecondPort, mResultIp, mResultPort, powerOnValue, externalSensor, earthDisaster, type,mServerAdress, mServerPort);
        //设置发送命令的集合
        CommandSendManage.getInstance().setSendCommandList(sendCommandList);
        //开始发送命令
        CommandSendManage.getInstance().startSendCommand();

        //8s后加载动画还在就取消设置，设置失败
        myHandler = new MyHandler(this);
        myHandler.sendEmptyMessageDelayed(MSG_SET_UP_DEFAIL,8000);

    }

    //设置全部命令成功
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void setupAllCommandSucc(DeviceUpdateEvent.DeviceCommandSendStatus event) {
        int status=event.getStatus();
        String function=event.getFunction();
        if(isSetupDefail)//3s内没有得到消息显示设置失败，防止数据延迟之后回来设置成功
            return;
        if(status==2){//其中某一条命令发送超时，则停止发送，显示设置失败
            stopAnim();
            Toast.makeText(context,function+"设置失败", Toast.LENGTH_SHORT).show();
        }else if(status==3){
            //完成所有命令的发送
            //结束动画
            stopAnim();
            Toast.makeText(context,R.string.toast_setup_success, Toast.LENGTH_SHORT).show();
            backTo();
            //最后再发送一次GETALL获取最新数据
            //得到发送命令的集合
            ArrayList<TagSendCommand> sendCommandList= GetSendCommandList.getGetAllCommandList();
            //设置发送命令额集合
            CommandSendManage.getInstance().setSendCommandList(sendCommandList);
            //开始发送命令
            CommandSendManage.getInstance().startSendCommand();
        }
    }


    //当Activity与fragment建立关联时调用
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context=context;
    }



    private int GetDecimalNumber(String date) {
        int value = 0;
        switch (date){
            case "一":
                value= (int) Math.pow(2,0);
                break;
            case "二":
                value= (int) Math.pow(2,1);
                break;
            case "三":
                value= (int) Math.pow(2,2);
                break;
            case "四":
                value= (int) Math.pow(2,3);
                break;
            case "五":
                value= (int) Math.pow(2,4);
                break;
            case "六":
                value= (int) Math.pow(2,5);
                break;
            case "日":
                value= (int) Math.pow(2,6);
                break;
        }
        return value;
    }




    @Subscribe(threadMode = ThreadMode.MAIN)
    public void showSetupOtherCommunication(OtherCommunicationDialogDismissEvent event) {
        String type=event.getName();
        tvCommunicationSetup.setText(type);
        if(type.equals(getString(R.string.tcp_client)) || type.equals(getString(R.string.tcp_server))){
            clCommunication.setVisibility(VISIBLE);
        }else{
            clCommunication.setVisibility(View.GONE);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void showSetupRecordingInterval(TimeZoneDialogDismissEvent event) {
        tvRecordingInterval.setText(event.getName());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void showSelectDataLink(WifiFrequencyBandDialogDismissEvent event) {
        String model=event.getName();
        tvDataLink.setText(model);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void showSetupSurveyPowerOnData(PowerOnDataDialogDismissEvent event) {
        //显示星期一、二
        ArrayList<String> arrayList = event.getArrayList();
        String name = "";
        for (String str : arrayList) {
            name = name + str.substring(2) + "、";
        }
        tvPowerOnData.setText("星期" + name.substring(0,name.length()-1));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void showSetupSurveyPowerOnTime(PowerOnTimeDialogDismissEvent event) {
        tvPowerOnTime.setText(event.getName());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void showSetupSurveyWorkerHours(WorkerHoursDialogDismissEvent event) {
        tvWorkerHours.setText(event.getName());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void showSetupSurveyPowerOffElectricQuantity(PowerOffElectricQuantityDialogDismissEvent event) {
        tvPowerOffElectricQuantity.setText(event.getName());
    }

    @Override
    protected void createPresenter(List<BasePresenter> mPresenters) {

    }

    @Override
    public void onDestroy() {
        if(myHandler!=null){
            myHandler.removeCallbacksAndMessages(null);
            myHandler=null;
        }
        stopAnim();
        super.onDestroy();
    }
}
